using System;
using System.Collections;

namespace codebase.UI
{
    public class ListBox : Control
    {
        public int ForeColor;
        public int BorderColor = ColorExtensions.BLACK16;
        public int SelectedColor = ColorExtensions.RGB16(120, 185, 135);

        public string[] Items;
        ArrayList _Items = new ArrayList();

        public int lineBufferSize = 150;
        public int scrollPosition = 0;
        public bool scrollAutomatic = true;

        public int Padding = 1;

        public ListBox(int x, int y,int w, int h, int bg, int tc,string[] list, Window parent)
        {
            Init(x, y,w,h,bg,tc,list, parent);
        }

        void Init(int x, int y,int w, int h, int bg, int tc,string[] list, Window parent)
        {
            Parent = parent;

            Left = x * Parent.Parent.screen.CharacterWidth;
            Top = y * Parent.Parent.screen.CharacterHeight;
            Width = w * Parent.Parent.screen.CharacterWidth;
            Height = h * Parent.Parent.screen.CharacterHeight;

            Items = list;

            BackgroundColor = bg;
            ForeColor = tc;
        }

        public void scrollUp()
        {
            if (scrollPosition > 0)
            {
                scrollPosition -= 1;
            }
        }

        public void scrollDown()
        {
            int pos = Height / Parent.Parent.screen.CharacterHeight;
            pos -= 2;

            if (scrollPosition < Items.Length - pos)
            {
                scrollPosition += 1;
            }
        }

        public void addItem(string item)
        {
            if (_Items.Count >= lineBufferSize)
            {
                _Items.RemoveAt(0);
            }

            _Items.Add(item);

            if (_Items.Count >= lineBufferSize)
            {
                _Items.RemoveAt(0);
            }

            refreshData();

            if (scrollAutomatic)
            {
                int pos = Height / Parent.Parent.screen.CharacterHeight;
                pos -= 2;

                if (Items.Length < pos)
                {
                    scrollPosition = 0; 
                }
                else
                {
                    scrollPosition = Items.Length - pos;
                }
            }
        }

        public void deleteItem(int index)
        {
            _Items.RemoveAt(index);
            refreshData();
        }

        public void clear()
        {
            _Items.Clear();
            Items = null;
            scrollPosition = 0;
        }

        void refreshData()
        {
            Items = new string[_Items.Count];
            int i = 0;
            foreach (object o in _Items)
            {
                Items[i] = (string)o;
                i++;
            }
        }

        protected override void DrawAll()
        {
            Parent.Parent.screen.DrawFilledRectangle(Left + Parent.Left + Padding, Top + Parent.Top + Padding, Left + Width + Parent.Left - Padding, Top + Height + Parent.Top - Padding, BackgroundColor);

            Draw();

            Parent.Parent.screen.DrawRectangle(Left + Parent.Left + Padding, Top + Parent.Top + Padding, Left + Width + Parent.Left - Padding, Top + Height + Parent.Top - Padding, BorderColor);
        }

        protected override void Draw()
        {
            if (Items == null) return;

            Parent.Parent.screen.DrawFilledRectangle(Left + Parent.Parent.screen.CharacterWidth + Parent.Left, Top + Parent.Parent.screen.CharacterHeight + Parent.Top, Left + Width - Parent.Parent.screen.CharacterWidth + Parent.Left, Top + Height - Parent.Parent.screen.CharacterHeight + Parent.Top, BackgroundColor);

            int lines = (Height / Parent.Parent.screen.CharacterHeight) -2;

            for (int i = 0; i < lines; i++)
            {
                int s = scrollPosition + i;
                if (s < Items.Length)
                {
                    int pos = (i + 1) * Parent.Parent.screen.CharacterHeight;

                    if (s == Items.Length - 1)
                    {
                        Parent.Parent.screen.DrawFilledRectangle(Left + Parent.Left + Parent.Parent.screen.CharacterWidth, Top + Parent.Top + pos, Left + Parent.Left + Width - Parent.Parent.screen.CharacterWidth, Top + Parent.Top + Parent.Parent.screen.CharacterHeight + pos, SelectedColor);
                        Parent.Parent.screen.DrawText(Items[s], Left + Parent.Left + Parent.Parent.screen.CharacterWidth, Top + Parent.Top + pos, false, SelectedColor, ForeColor);
                    }
                    else
                    {
                        Parent.Parent.screen.DrawText(Items[s], Left + Parent.Left + Parent.Parent.screen.CharacterWidth, Top + Parent.Top + pos, false, BackgroundColor, ForeColor);
                    }
                }
            }
        }
    }
}
